
local LoginSceneCCB = class("LoginSceneCCB", cc.load("mvc").ViewBase)

function LoginSceneCCB:ctor()
    local httpRequest = require("app.models.network.HttpRequest")
    local websocket = require("app.models.ManagerNet"):getInstance()
    httpRequest.launch(websocket)

    LoginSceneCCB.super.ctor(self) 
    --这里还是不要用self.super 因为LoginSceneCCB是最外层类所以不会报错 如果外层还有类 这里的self和LoginSceneCCB 不是一个意义
end

function LoginSceneCCB:onCreate()
   
   self.luafilePath = "views.LoginSceneCCB"
   self:getCcbNode():addTo(self)
   _createNameEditBox(self)
   self.setControlButtonTitle(self.controlButton1, Word(0).LoginSceneCCB_03)
   self.setControlButtonTitle(self.controlButton2, Word(0).LoginSceneCCB_04)

end

function LoginSceneCCB:onEnter_()
    ManagerNotification:registerObserver("user.login",self,handler(self, _onLogin) )
end

function LoginSceneCCB:onExit_()
    LoginSceneCCB.super.onExit_()
    ManagerNotification:unregisterObserver("user.login",self)
end

function LoginSceneCCB.loginCall1(sender,controlEvent,self)
    ManagerUserData.loginMode = 1
    _launcher(self)
end

function LoginSceneCCB.loginCall2(sender,controlEvent,self)
    ManagerUserData.loginMode = 2
    _launcher(self)
end

_onLogin = function(self,characterList)
    local shortLivedData = ManagerCookie.getValue(enumlCookieFlag.shortLivedData, "myGame") or {}
    shortLivedData.lastLoginId = self.userId
    ManagerCookie.setValue(enumlCookieFlag.shortLivedData,shortLivedData, "myGame")
    
   if characterList then
        local shortLivedData = ManagerCookie.getValue(enumlCookieFlag.shortLivedData) or {}
        local lastCharacterId = shortLivedData["lastCharacterId" .. ManagerUserData.loginMode]
        MyApp:enterScene("ChooseSceneCCB",{showId = lastCharacterId})
    else
        MyApp:enterScene("CreateSceneCCB")
    end
end

_launcher = function(self)
    if not self.userId or self.userId == "" then 
        Tips.ShowTTF({str = Word(0).LoginSceneCCB_02})
    else
        ManagerUserData.userData.userBaseInfo.userId = self.userId
        AdapterSendMsg:sendMsg("user.login",{userId=self.userId})
    end
end

_createNameEditBox = function(self)
    --editbox
    local editBoxSize = cc.size(self.inputBox:getContentSize().width-30, 40)
    local editBox  = cc.EditBox:create(editBoxSize, cc.Scale9Sprite:create())
    
    editBox:setPosition(15, self.inputBox:getContentSize().height / 2)
    editBox:setFontSize(30)
    editBox:setPlaceHolder(Word(0).LoginSceneCCB_01)
    editBox:setMaxLength(8)
    editBox:setAnchorPoint(cc.p(0, 0.5))
    editBox:setFontColor(Constants.color2 )
    editBox:setPlaceholderFontColor(Constants.color2 )
    editBox:setPlaceholderFontSize(30)
    editBox:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE)
    editBox:setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE)

    editBox:addTo(self.inputBox)

    local shortLivedData = ManagerCookie.getValue(enumlCookieFlag.shortLivedData, "myGame") or {}
    if shortLivedData.lastLoginId then 
        editBox:setText(shortLivedData.lastLoginId)
        self.userId = shortLivedData.lastLoginId
    end

    --Handler
    local editBoxTextEventHandle = function(strEventName,pSender)
        local edit = pSender
        local strFmt 
        if strEventName == "began" then
        elseif strEventName == "ended" then
        elseif strEventName == "return" then
        elseif strEventName == "changed" then
            strFmt = string.format("editBox %p TextChanged, text: %s ", edit, edit:getText())
            self.userId = edit:getText()
            print(strFmt)
        end
    end

    editBox:registerScriptEditBoxHandler(editBoxTextEventHandle)
end


return LoginSceneCCB
